
/*bertrand first order conditions for logit demand.  
s a number of brands x 1 vector of pre-merger volume shares 
p a nbrnds x 1 vector of pre-merger prices
b is a scalar price coefficient from the logit model.  It is the marginal utility of income (a positive number).
e is the "aggregate elasticity of demand for inside goods".  It should be positive.
preM is a nbrands x nbrands matrix of dummies, with 1's on the diagonal and entry (i,j) equal to 1 if brands i and j are 
produced by the same firm pre-merger.
postM is a nbrands x nbrands matrix of dummies, with 1's on the diagonal and entry (i,j) equal to 1 if brands i and j are 
produced by the same firm post-merger.
Reference is Werden and Froeb "The Effects of Mergers in Differentiated Products Industries: Logit Demand and Merger Policy", 
JLEO, V10 N2*/

mata
function mcosts_antisyr(x,s,p,b,e,preM,postM)
{
    /*create intercepts such that premerger prices predict pre-merger shares perfectly*/
    n=rows(s)
    antifoc=J(n,cols(x),0)
    pbar=s'*p
    prn=e/(b*pbar)
    pr=J(n+1,1,0)
    pr[n+1,1]=prn
    for(i=1;i<=n;i++){
        pr[i,1]=s[i,1]*(1-prn)
    }
    cept=J(n+1,1,0)
    /*arbitrarily set intercept of outside option equal to 1*/	
    cept[n+1,1]=1
    for(i=1;i<=n;i++){
        cept[i,1]=cept[n+1,1]+b*p[i,1]+ln(pr[i,1]/prn)
    }
    /*for(i=1;i<=n;i++){
        c[i,1]=p[i,1]-pbar*1/(b*pbar*(1-s[i,1])+e*s[i,1])
    }	

    for(i=1;i<=n;i++){
        c[i,1]=p[i,1]-pbar*1/(b*pbar*(1-preM[i,.]*s[.,1])+e*preM[i,.]*s[.,1])
    }*/	

	prs=J(n+1,1,0)
    temp=exp(cept-b:*(p[.,1]\0))	
	for(i=1;i<=n;i++){
            prs[i,1]=exp(cept[i,1]-b*p[i,1])/(J(n+1,1,1)'*temp)
    }
	prs[n+1,1]=1-J(n,1,1)'*prs[1..n,1]		
		
	for(j=1;j<=cols(x);j++){
        antifoc[.,j]=-(p-x[.,j])+(1/b):*(1:/(J(n,1,1)-postM*prs[1..n,1]))
    } 	
return(antifoc)
}
end
